import R from "ramda"

// Inplementation of compose
const compose = <T1, T2, T3>(f: (v: T2) => T3, g: (v: T1) => T2) =>
    (v: T1) => f(g(v));

// ramda 官方版本用不了，外部数据类型和 field 类型退化为 any 即可
const prop = <P extends string>(p: P): ((obj: any) => any) =>
    obj => obj[p]

// Point-free style
const resultEquals20 =
    R.filter(
        compose(
            R.equals(20),
            prop("result")
        )
    )

type Count = {
    result: number
}
let cs: Count[] = [{ result: 20 }, { result: 20 }, { result: 10 }]
console.log(resultEquals20(cs))


type Wrong = {
    res: number
}
let ws: Wrong[] = [{ res: 20 }, { res: 20 }, { res: 20 }]
console.log(resultEquals20(ws))

let w: Wrong = { res: 10 }
console.log(prop("result")(w))  // undefined 是我们想要的，而不是报错